package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"week52/client/client"
	"week52/client/request"
	"week52/common/proto/class"
)

func AddClass(c *gin.Context) {
	var data request.AddClass
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}
	addClass, err := client.AddClass(c, &class.AddClassReq{
		Name:    data.Name,
		Address: data.Address,
		Types:   int32(data.Types),
		Num:     int32(data.Num),
		Price:   data.Price,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": addClass,
	})
}

func ClassDetail(c *gin.Context) {
	var data request.ClassDetail
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}
	detail, err := client.ClassDetail(c, &class.ClassDetailReq{Id: int32(data.Id)})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": detail,
	})
}

func AddClassOrder(c *gin.Context) {
	var data request.AddClassOrder
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}
	order, err := client.AddClassOrder(c, &class.AddClassOrderReq{
		ClassId: int32(data.ClassId),
		Dates:   data.Dates,
		Times:   data.Times,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": order,
	})
}

func TrueOrder(c *gin.Context) {
	var data request.TrueOrder
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}
	order, err := client.TrueOrder(c, &class.TrueOrderReq{
		Writers: data.Writers,
		Id:      int32(data.Id),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": order,
	})
}

func StatusOrderList(c *gin.Context) {
	var data request.StatusOrderList
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}
	list, err := client.StatusOrderList(c, &class.StatusOrderListReq{Status: int32(data.Status)})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": list,
	})
}

func TellSmall(c *gin.Context) {
	var data request.TellSmall
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}
	small, err := client.TellSmall(c, &class.TellSmallReq{Id: int32(data.Id)})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": small,
	})
}
